﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation;

namespace BoardGamePlayer
{
    /// <summary>
    /// Interaction logic for Card.xaml
    /// </summary>
    public partial class Card : ScatterViewItem
    {

        private string cardFaceSource;
        private string cardBackSource;
        private bool isFaceUp;
        private System.Timers.Timer doubleTapTimer;
        private Deck parentDeck;

        public Card()
        {
            InitializeComponent();
            CardFaceSource = "";
            IsFaceUp = false;
            initializeTimer();
        }

        public Card(string face)
        {
            InitializeComponent();
            CardFaceSource = face;
            IsFaceUp = false;
            initializeTimer();
        }

        public Card(string face, string back) 
        {
            InitializeComponent();
            CardFaceSource = face;
            CardBackSource = back;
            IsFaceUp = false;
            initializeTimer();
        }

        public Deck ParentDeck 
        {
            get { return parentDeck; }
            set { parentDeck = value; }
        }

        private void initializeTimer() 
        {
            doubleTapTimer = new System.Timers.Timer(500);
            doubleTapTimer.Enabled = false;
            doubleTapTimer.Elapsed += new ElapsedEventHandler(handleElapsedEvent);
        }

        public ImageSource FaceUpImage  
        {
            get { return IsFaceUp ? cardFace.Source : cardBack.Source; }
        }
         
        private string CardFaceSource 
        {
            set 
            { 
                cardFaceSource = value;
                try
                {
                    cardFace.Source = new BitmapImage(new Uri(value));
                }
                catch (System.UriFormatException e)
                {
                    cardFace.Source = null;
                }

            }
        }

        private string CardBackSource
        {
            set
            {
                cardBackSource = value;
                try
                {
                    cardBack.Source = new BitmapImage(new Uri(value));
                }
                catch (System.UriFormatException e)
                {
                    cardBack.Source = null;
                }

            }
        }

        public bool IsFaceUp 
        {
            get { return isFaceUp; }
            set 
            {
                isFaceUp = value;
                if (isFaceUp)
                {
                    cardFace.Visibility = Visibility.Visible;
                    cardBack.Visibility = Visibility.Hidden;
                }
                else 
                {
                    cardFace.Visibility = Visibility.Hidden;
                    cardBack.Visibility = Visibility.Visible;
                }
            }
        }

        private void handleElapsedEvent(object sender, ElapsedEventArgs e)
        {
            doubleTapTimer.Enabled = false;
        }

        public void flipCard() 
        {
            IsFaceUp = !IsFaceUp;
        }

        protected override void OnManipulationStarting(ManipulationStartingEventArgs e)
        {
            BeginDragDrop(this);
            base.OnManipulationStarting(e);
        }

        private void ScatterViewItem_Drop(object sender, SurfaceDragDropEventArgs e)
        {
            //this 
            if (e.Cursor.Data is Card)
            {
                //combine the cards into a new deck
                List<Card> cardList = new List<Card>();
                cardList.Add(this);
                (e.Cursor.Data as Card).Width = this.ActualWidth;
                (e.Cursor.Data as Card).Height = this.ActualHeight;
                cardList.Add((Card)e.Cursor.Data);
                Deck newDeck = new Deck(cardList);
                newDeck.Center = this.ActualCenter;
                newDeck.Orientation = this.ActualOrientation;
                newDeck.Height = this.ActualHeight;
                newDeck.Width = this.ActualWidth;
                (Parent as ScatterView).Items.Add(newDeck);
                (Parent as ScatterView).Items.Remove(this);
            }
            else 
            {
                //just put it back where it came from
                (e.Cursor.DragSource as ScatterView).Items.Add(e.Cursor.Data);
            }

        }

        private void SurfaceButton_Click(object sender, RoutedEventArgs e)
        {
            flipCard();
        }
    }
}